//
//  FKSearchVC.m
//  BaiYeMallShop
//
//  Created by Macbook Pro on 2018/12/3.
//  Copyright © 2018 Macbook Pro. All rights reserved.
//

#import "FKSearchVC.h"
#import "FKSearchHistoryVC.h"
#import "FKGoodsListVC.h"
#import "FKHomeGoods.h"

@interface FKSearchVC ()<UITextFieldDelegate>

@property (nonatomic, weak) UITextField *searchField;
@property (nonatomic, weak) FKSearchHistoryVC *historyVc;
@property (nonatomic, weak) FKGoodsListVC *resultVc;

@property (nonatomic, assign) BOOL beginInput;

@end

@implementation FKSearchVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem fk_itemWithImageName:@"back_arrow_gray" highImageName:@"back_arrow_gray" target:self action:@selector(back)];
    
    UIButton *searchBtn = [UIButton fk_btnWithTarget:self action:@selector(searchBtnClicked) font:fk_adjustFont(14) normalTitle:@"搜索" normalTitleColor:fkColor333333 selectedTitle:nil selectedTitleColor:nil normalImg:nil selectedImg:nil bgImg:nil selectedBgImg:nil];
    searchBtn.fk_height = 44;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:searchBtn];
    

//    self.navigationItem.rightBarButtonItem = [UIBarButtonItem fk_itemWithTarget:self action:@selector(searchBtnClicked) text:@"搜索" textColor:fkColor333333 font:fk_adjustFont(14)];
    
    UITextField *searchField = [[UITextField alloc] init];
    self.searchField = searchField;
    UIImageView *leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search_icon_search"]];
    searchField.placeholder = @"请输入关键字进行搜索";
    searchField.font = fk_adjustFont(13);
    leftView.contentMode = UIViewContentModeCenter;
    leftView.frame = CGRectMake(0, 0, fk_adjustW(16), fk_adjustW(16));
    searchField.leftView = leftView;
    searchField.leftViewMode = UITextFieldViewModeAlways;
    searchField.clearButtonMode = UITextFieldViewModeWhileEditing;
    searchField.delegate = self;
//    CGFloat fieldW = fkScreenW - 40 - cancelW;
    CGFloat fieldW = fk_adjustW(285);
//    CGFloat fieldW = fk_adjustW(320);
    searchField.frame = CGRectMake(0, 0, fieldW, fk_adjustW(30));
    searchField.backgroundColor = [UIColor fk_colorWithHex:0xF5F5F5];
    [searchField fk_viewCornerRadius:fk_adjustW(15) borderWidth:0 borderColor:nil];
    [searchField addTarget:self action:@selector(fieldValueChanged:) forControlEvents:UIControlEventEditingChanged];
    self.navigationItem.titleView = searchField;
    
    FKSearchHistoryVC *historyVc = [[FKSearchHistoryVC alloc] init];
    [self addChildViewController:historyVc];
    self.historyVc = historyVc;
    [self.view addSubview:historyVc.view];
    historyVc.view.frame = self.view.bounds;
    historyVc.keywordClickBlock = ^(NSString * _Nonnull keyword) {
        self.searchField.text = keyword;
        [self search];
    };
    
    FKGoodsListVC *resultVc = [[FKGoodsListVC alloc] init];
    [self addChildViewController:resultVc];
    self.resultVc = resultVc;
    [self.view addSubview:resultVc.view];
    resultVc.view.frame = self.view.bounds;
    resultVc.view.hidden = true;
    
    self.beginInput = true;
}

- (void)back
{
    fkPopVC;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    if (self.beginInput) {
        [self.searchField becomeFirstResponder];
        self.beginInput = false;
    }
}

#pragma mark - action
- (void)fieldValueChanged:(UITextField *)field
{
    // 算了，懒得给它做实时搜索了
    
    if (field.text.length == 0) {
        self.resultVc.view.hidden = true;
    }
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    self.resultVc.view.hidden = true;
    [self.historyVc loadData];
    return true;
}

#pragma mark - target
- (void)searchBtnClicked
{
    [self search];
}

#pragma mark - 网络请求
- (void)search
{
    [self.searchField endEditing:true];
    
    if (self.searchField.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"搜索内容不能为空"];
        return;
    }
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"title"] = self.searchField.text;
    param[@"page"] = @(0);
    param[@"pagesize"] = @(10);
    self.resultVc.param = param;
    self.resultVc.view.hidden = false;
    [self.resultVc search];
    
//    [SVProgressHUD show];
//    [FKHttpTool postWithSuffix:@"m=Home&c=Index&a=itemList" param:param success:^(FKBaseResult *result) {
//        [SVProgressHUD dismiss];
//        if (result.isSuccess) {
//            self.resultVc.view.hidden = false;
//
//            self.resultVc.datas = [FKHomeGoods mj_objectArrayWithKeyValuesArray:result.data[@"list"]];
//            [self.resultVc checkEmpty];
//            [self.resultVc.tableView reloadData];
//        }
//    } failure:nil];
}

@end
